import numpy as np
import pandas as pd
from os.path import isfile, join
from os import listdir
import os
import re
from matplotlib import pyplot as plt
chars = ['simplicity', 'comfort']
war = ['CW', 'WWI']

current_directory = os.getcwd()
parent_directory = os.path.dirname(current_directory)

# Navigate up one level

for z, char in enumerate(chars):
    if war[z] == 'CW':
        mypath = os.path.join(parent_directory, 'Data', 'perturbation_results_CW/')
        save_file = os.path.join(parent_directory, 'Data', \
            'perturbation_results_CW', 'synth_perturb_results_CW_' + char + '.csv')
    else:
        mypath = os.path.join(parent_directory, 'Data', 'perturbation_results/')
        save_file = os.path.join(parent_directory, 'Data', \
            'perturbation_results_CW', 'synth_perturb_results_' + char + '.csv')

    onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
    matching2 = [s for s in onlyfiles if "SynthResult" in s]
    matching = [s for s in matching2 if char in s]

    df = pd.DataFrame(columns=['diffindiff', 'PercentMislabelled', 'Std_dev'])
    for i, acc in enumerate(range(45, 101)):
        acc_matched = [s for s in matching if str(acc) in s]
        temp_df = pd.DataFrame()
        for file in acc_matched:
            if file == ".DS_Store":
                None
            else:
                num = re.findall(r'\d+', file)
                acc_level = num[0]
                loop_level = num[1]
                new_df = pd.read_stata(mypath + file)
                new_df['PercentMislabelled'] = 100 - int(acc_level)
                new_df['LoopLevel'] = int(loop_level)
                new_df = new_df[['diffindiff', 'PercentMislabelled']]
                temp_df = temp_df.append(new_df)
        df.loc[i] = temp_df['diffindiff'].mean(), temp_df['PercentMislabelled'].mean(), temp_df['diffindiff'].std()

    df.sort_values(by="PercentMislabelled", ascending=True).to_csv(save_file, index=False)
    df = df.sort_values(by="PercentMislabelled", ascending=True)
    path_deviation = df['Std_dev']
    plt.plot(df['diffindiff'].round(2), linewidth=2, color='maroon')
    plt.fill_between(df['PercentMislabelled'], (df['diffindiff'].round(2) - path_deviation.round(2)), (df['diffindiff'].round(2) + path_deviation.round(2)), color='gainsboro')
    plt.xlabel("Percent Mislabelled")
    plt.ylabel("Estimate for Inventor Effort Allocation on " + char)
    plt.title("Simulated Stability of Economic Estimate")
    plt.grid()

    if war[z] == 'CW':
        sav_dir = os.path.join(parent_directory, 'Figures',\
         "Stability_of_CW_Economic_Estimate_" + char + ".png")
        plt.savefig(sav_dir)
        plt.clf()
    else:
        sav_dir = os.path.join(parent_directory
        	, 'Figures',\
            "Stability_of_Economic_Estimate_" + char + ".png")
        plt.savefig(sav_dir)
        plt.clf()
